import { defineStore } from 'pinia'
import { pinia } from '../index'
import { HomeSet, UserInfo } from '@/api/login/types'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { loginOut, checkIsLogin, getLoginDatum } from '@/api/login'
import { useTagsViewStore } from './tagsView'
import router from '@/router'
import { loadRoute } from '@/router'
export interface UserState {
  homeSet: object
  powers: object
  isInit: boolean
  state: any
}

const checkLoginState = async () => {
  try {
    await checkIsLogin()
    return true
  } catch {
    return false
  }
}
export const useUserStore = defineStore('user', {
  state: (): UserState => {
    return {
      homeSet: {},
      powers: {},
      isInit: false,
      state: null
    }
  },
  getters: {
    power(): AppRouteRecordRaw[] {
      if (this.state == null) {
        return []
      }
      const sysId = this.state.sysId
      if (sysId == null || sysId == undefined) {
        return []
      }
      return this.powers[sysId]
    },
    home(): HomeSet | null {
      if (this.state.user == null) {
        return null
      }
      const sysId = this.state.sysId
      if (sysId == null || sysId == undefined) {
        return null
      }
      return this.homeSet[sysId]
    },
    user(): UserInfo {
      return this.state.user
    },
    sysId(): string | null | undefined {
      return this.state.user?.CurSubSysId
    },
    comId(): string | null | undefined {
      return this.state.user?.CompanyId
    }
  },
  actions: {
    getComName(comId: string): string | null {
      if (this.state.user) {
        return this.state.user.Company[comId]
      }
      return null
    },
    setCurSysId(id: string): HomeSet | null {
      this.state.setSysId(id)
      return this.homeSet[id]
    },
    logoutConfirm() {
      const { t } = useI18n()
      ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
        confirmButtonText: t('common.ok'),
        cancelButtonText: t('common.cancel'),
        type: 'warning'
      })
        .then(async () => {
          await loginOut()
          this.logout()
        })
        .catch(() => {})
    },
    reset() {
      const tagsViewStore = useTagsViewStore()
      tagsViewStore.delAllViews()
      this.state?.logout()
      this.homeSet = {}
      this.powers = {}
      this.isInit = false
    },
    logout() {
      this.reset()
      router.replace('/login')
    },
    setState(state) {
      this.state = state
    },
    async loadUser() {
      if (this.state.user == null) {
        const user = await getLoginDatum()
        this.state.setUser(user)
      }
      const user = this.state.user
      user.SubSystem.forEach((a) => {
        const list = user.Power[a.Id]
        this.powers[a.Id] = loadRoute(list)
        this.homeSet[a.Id] = a.Home
      })
    },
    async checkState() {
      if (!this.state.isLogin()) {
        return false
      } else if (!(await checkLoginState())) {
        return false
      } else {
        return (await this.init()) !== 0
      }
    },
    async init(): Promise<number> {
      if (!this.state.isLogin()) {
        return 0
      } else if (!this.isInit) {
        this.isInit = true
        await this.loadUser()
        return 1
      }
      return 2
    }
  },
  persist: false
})

export const useUserStoreWithOut = () => {
  return useUserStore(pinia)
}
